package work.xiaohong.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.mybatisplus.entity.Role;
import work.xiaohong.mybatisplus.service.IRoleService;
import work.xiaohong.utils.Result;
import work.xiaohong.utils.StatusCode;

import java.util.List;

/**
 * 用户角色表 前端控制器
 * @author xiaohong
 * @since 2021-10-22 10:10:22
 */
@Api(tags = "Role接口文档")
@RestController
@RequestMapping("/role")
public class RoleController {

   @Autowired
   private IRoleService roleService;


    /***
    * 条件分页查询
    * @param role
    * @param page
    * @param size
    * @return
    */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "Role对象",required = true) Role role, @PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        Page<Role> pageInfo = roleService.page(new Page<Role>(page,size),new QueryWrapper<Role>(role));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }


    /***
    * 分页查询
    * @param page:当前页
    * @param size:每页显示多少条
    * @return
    */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用RoleService实现分页查询Role
        Page<Role> pageInfo = roleService.page(new Page<>(page,size));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
    * 条件查询
    * @param role
    * @return
    */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<Role>> findList(@RequestBody(required = false) @Parameter(name = "Role对象",required = true) Role role){
        List<Role> list = roleService.list(new QueryWrapper<Role>(role));
        return new Result<List<Role>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }


    /***
    * 根据Id删除role数据
    * @param id
    * @return
    */
    @Operation(summary = "根据ID删除Role",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable(value="id") Integer id){
        roleService.removeById(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }



    /***
    * 根据Id批量删除role数据
    * @param ids
    * @return
    */
    @Operation(summary = "根据Id批量删除Role",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的Role对象id集合",required = true) List<Integer> ids){
        roleService.removeByIds(ids);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }


    /***
    * 根据ID修改Role数据
    * @param role
    * @return
    */
    @Operation(summary = "根据ID修改Role",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "Role对象",required = true) Role role){
        roleService.updateById(role);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }


     /***
     * 新增Role数据
     * @param role
     * @return
     */
     @Operation(summary = "添加Role",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping
     public Result add(@RequestBody  @Parameter(name = "Role对象",required = true) Role role){
        roleService.save(role);
        return new Result(true,StatusCode.OK.getCode(),"添加成功");
     }

     /***
     * 根据ID查询Role数据
     * @param id
     * @return
     */
      @Operation(summary = "根据ID查询Role",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
      @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
     @GetMapping("/{id}")
     public Result<Role> findById(@PathVariable(value="id") Integer id){
        Role role = roleService.getById(id);
        return new Result<Role>(true,StatusCode.OK.getCode(),"查询成功",role);
     }

     /***
     * 根据Id批量查询Role数据
     * @param ids
     * @return
     */
     @Operation(summary = "根据Id批量查询Role数据",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping("/ids")
     public Result<List<Role>> findByIds(@RequestBody @Parameter(name = "需要查询的Role对象id集合",required = true) List<Integer> ids){
        List<Role> roleList = roleService.listByIds(ids);
        return new Result<List<Role>>(true,StatusCode.OK.getCode(),"查询成功",roleList);
     }

     /***
     * 查询Role全部数据
     * @return
     */
     @Operation(summary = "查询所有Role",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @GetMapping
     public Result<List<Role>> findAll(){
        List<Role> list = roleService.list();
        return new Result<List<Role>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
     }


}
